package com.clz.latte.net;

import android.content.Context;

import com.clz.latte.net.callback.IError;
import com.clz.latte.net.callback.IFailure;
import com.clz.latte.net.callback.IRequest;
import com.clz.latte.net.callback.ISuccess;
import com.clz.latte.net.callback.RequestCallBacks;
import com.clz.latte.net.download.DownLoadHandler;
import com.clz.latte.ui.loader.LatteLoader;
import com.clz.latte.ui.loader.LoaderStyle;

import java.io.File;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;

/**
 * Created by Administrator on 2017/11/21.
 */

public class RestClient {
    private final String URL;
    private static final Map<String, Object> PARAMS = RestCreator.getParams();
    private final RequestBody REQUEST_BODY;
    private final File FILE;
    private final String DOWN_LOAD_DIR;//下载文件的路径
    private final String EXTENSION;//下载文件的后缀名
    private final String NAME;//下载文件的名称
    private final IRequest REQUEST;
    private final ISuccess SUCESS;
    private final IError ERROR;
    private final IFailure FAILURE;
    private final Context CONTEXT;
    private final LoaderStyle LOADER_STYLE;

    public RestClient(String URL,
                      Map<String, Object> params,
                      IRequest request,
                      ISuccess success,
                      IError error,
                      IFailure failure,
                      RequestBody requestBody, File file, String downLoadDir, String extension, String name, Context context,
                      LoaderStyle loaderStyle) {
        this.URL = URL;
        this.PARAMS.putAll(params);
        this.REQUEST = request;
        this.SUCESS = success;
        this.ERROR = error;
        this.FAILURE = failure;
        this.REQUEST_BODY = requestBody;
        this.FILE = file;
        this.DOWN_LOAD_DIR = downLoadDir;
        this.EXTENSION = extension;
        this.NAME = name;
        this.CONTEXT = context;
        this.LOADER_STYLE = loaderStyle;
    }

    public static RestClientBuilder builder() {
        return new RestClientBuilder();
    }

    private void request(HttpMethod method) {
        final RestServices service = RestCreator.getRetrofitService();
        if (REQUEST != null) {
            REQUEST.onRequestStart();
        }
        if (LOADER_STYLE != null) {
            LatteLoader.showLoading(CONTEXT, LOADER_STYLE);
        }
        Call<String> call = null;
        switch (method) {
            case GET:
                call = service.get(URL, PARAMS);
                break;
            case POST:
                call = service.post(URL, PARAMS);
                break;
            case POST_RAW:
                call = service.postRaw(URL, REQUEST_BODY);
                break;
            case PUT:
                call = service.put(URL, PARAMS);
                break;
            case PUT_RAW:
                call = service.putRaw(URL, REQUEST_BODY);
                break;
            case UPLOAD:
                RequestBody requestBody =
                        RequestBody.create(MediaType.parse(MultipartBody.FORM.toString()), FILE.getName());
                MultipartBody.Part body = MultipartBody.Part.createFormData("file", FILE.getName(), requestBody);
                call = service.upload(URL, body);
                break;
            case DELET:
                call = service.delete(URL, PARAMS);
                break;
            default:
                break;
        }
        call.enqueue(getRequestCallBacks());
    }

    private Callback<String> getRequestCallBacks() {
        return new RequestCallBacks(
                REQUEST, SUCESS, ERROR, FAILURE, LOADER_STYLE);
    }

    public final void get() {
        request(HttpMethod.GET);
    }

    public final void post() {
        if (REQUEST_BODY == null) {
            request(HttpMethod.POST);
        } else {
            if (!PARAMS.isEmpty()) {
                throw new RuntimeException("params must be null!");
            }
            request(HttpMethod.POST_RAW);
        }
    }

    public final void put() {
        if (REQUEST_BODY == null) {
            request(HttpMethod.PUT);
        } else {
            if (!PARAMS.isEmpty()) {
                throw new RuntimeException("params must be null!");
            }
            request(HttpMethod.PUT_RAW);
        }
    }

    public final void delete() {
        request(HttpMethod.DELET);
    }

    public final void upload() {
        request(HttpMethod.UPLOAD);
    }

    public final void downLoad() {
        new DownLoadHandler(URL, DOWN_LOAD_DIR, EXTENSION, NAME,
                REQUEST, SUCESS, ERROR, FAILURE).handleDownLoad();
    }
}
